#! /usr/bin/env perl

###########################################################################
#
# vget
#
###########################################################################
#
# This command is completely unnecessary from a version control standpoint.  However, it can be useful in that
# it does a few things, all of them optional:
#
#	*	It will reset the date on the file to the last checked in date, which vbuild does not (currently) do.
#
#	*	It will filter the file through any PostGet filter specified in the conf file.
#
#	*	It will set a marker to indicate that you are currently working on the file, if the conf file
#		specifies that and the VC system supports it (e.g. cvs edit).
#
# #########################################################################
#
# All the code herein is released under the Artistic License
#		( http://www.perl.com/language/misc/Artistic.html )
# Copyright (c) 2000-2007 Barefoot Software
#
###########################################################################

use strict;
use warnings;

use VCtools::Base;
use VCtools::Args;
use VCtools::Common;


#################################
# OPTIONS AND ENVIRONMENT VARS
#################################

VCtools::args('files', 'list', 'file(s) to get');
VCtools::getopts();

my @files = VCtools::files();

#################################
# CHECK FOR ERRORS
#################################

my $proj = VCtools::verify_files_and_group(@files);

# get statuses for everything at once (quicker that way)
VCtools::cache_file_status(@files, { DONT_RECURSE => 1 });

foreach my $file (@files)
{
	# can't get a directory
	VCtools::fatal_error("cannot get a directory") if -d $file;

	# can't get a file that isn't in VC already
	VCtools::fatal_error("$file is not in VC") unless VCtools::exists_in_vc($file);

	# can't get a file that already has mods
	VCtools::fatal_error("file $file has modifications; getting would overwrite those") if VCtools::modified_from_vc($file);

=not_working_with_svn_yet
	VCtools::fatal_error("$file has already been gotten") if VCtools::user_is_a_locker($file);

	my @lockers = cvs::lockers($file);
	if (@lockers)
	{
		# inform user that others are editing the file
		VCtools::prompt_to_continue("$file is currently being edited by: @lockers:);
	}
=cut
}


#################################
# MAIN
#################################

=not_working_with_svn_yet
if ( $revdate || $rev )
{ 
	# this is a precaution, as I f#cked myself with conflicts by 
	# checking out older version on top of edited new version
	die "$me: can not check out older version if editing."
			if cvs::user_is_a_locker($module);

	# -f gets the latest version if -r or -D fail
	my $options = "-f";
	$options = $options . " -D $revdate" if $revdate;
	$options = $options . " -r $rev" if $rev;
	print "$me: Syncing $module using $options.\n" if $verbose;
	system("cvs -r -d $cvsroot update $options $module");
}
else
{
	# -A resets any stick tags
	print "$me: Syncing $module to latest version.\n" if $verbose;
	system("cvs -r -d $cvsroot update -A $module");
}
=cut

=not_working_with_svn_yet
print "$me: Adding to watch and editors list.\n" if $verbose;
system("cvs -d $cvsroot edit $module");
system("cvs -d $cvsroot watch add -a edit $module");
=cut

foreach my $file (@files)
{
	VCtools::commit_files($proj, @files, { MESSAGE => VCtools::message() } );
}

print "done\n" unless $?;
